package com.jed.xxh.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.jed.xxh.R;
import com.jed.xxh.entity.MyUser;
import com.jed.xxh.utils.L;
import com.jed.xxh.utils.T;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * 项目名：SmartButler<br/>
 * 包名：  com.jed.xxh.ui<br/>
 * 作者：徐晓辉<br/>
 * 创建日期：2018/11/26 11:52<br/>
 * 描述：<br/>
 */
public class ForGetPasswordActivity extends BaseActivity implements View.OnClickListener {

    private EditText forget_et_now_password;//旧密码
    private EditText forget_et_new_password;//新密码
    private EditText forget_et_password_again;//确认密码
    private Button forget_btn_editor_password;//修改密码按钮
    private EditText forget_et_email;//邮箱
    private Button forget_btn_password;//忘记密码按钮

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        initView();
    }

    /**
     * 初始化
     */
    private void initView() {
        forget_et_now_password = findViewById(R.id.forget_et_now_password);
        forget_et_new_password = findViewById(R.id.forget_et_new_password);
        forget_et_password_again = findViewById(R.id.forget_et_password_again);
        forget_btn_editor_password = findViewById(R.id.forget_btn_editor_password);
        forget_et_email = findViewById(R.id.forget_et_email);
        forget_btn_password = findViewById(R.id.forget_btn_password);
        forget_btn_editor_password.setOnClickListener(this);
        forget_btn_password.setOnClickListener(this);


    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.forget_btn_editor_password://修改密码
                String now_password = forget_et_now_password.getText().toString().trim();
                String new_password = forget_et_new_password.getText().toString().trim();
                String password_again = forget_et_password_again.getText().toString().trim();

                boolean isEmpty = !TextUtils.isEmpty(now_password) && !TextUtils.isEmpty(now_password) && !TextUtils.isEmpty(now_password);
                if (isEmpty) {
                    if (new_password.equals(password_again)) {

                        MyUser.updateCurrentUserPassword(now_password, new_password, new UpdateListener() {
                            @Override
                            public void done(BmobException e) {
                                if (e == null) {
                                    T.showShortToast(ForGetPasswordActivity.this
                                            .getString(R.string.reset_successfully));
                                    finish();
                                } else {
                                    T.showShortToast(ForGetPasswordActivity.this
                                            .getString(R.string.reset_failed));
                                }
                            }
                        });
                    } else {
                        T.showShortToast(this.getString(R.string.text_two_input_not_consistent));
                    }
                } else {
                    T.showShortToast(this.getString(R.string.text_tost_empty));
                }
                break;
            case R.id.forget_btn_password://找回密码
                final String email = forget_et_email.getText().toString().trim();
                if (!TextUtils.isEmpty(email)) {
                    L.i("找回密码==email:" + email);
                    MyUser.resetPasswordByEmail(email, new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e == null) {
                                T.showShortToast(ForGetPasswordActivity.this
                                        .getString(R.string.text_email_send_ok) + email);
                            } else {
                                T.showShortToast(ForGetPasswordActivity.this
                                        .getString(R.string.text_email_send_no));
                            }
                        }
                    });

                } else {
                    T.showShortToast(this.getString(R.string.text_email_empty));
                }
                break;
        }
    }
}
